package com.hqyj.smvc.handler;

import com.github.pagehelper.PageInfo;
import com.hqyj.smvc.pojo.Employee;
import com.hqyj.smvc.pojo.Role;
import com.hqyj.smvc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleHandler {
    @Autowired
    private RoleService roleService;

    /**
     * 查看所有的角色的方法
     * 分页查询
     * @param pageNum 当前页
     * @param model 域对象
     * @return 返回一个角色集合，存入域对象中，在jsp页面中去遍历，显示到页面上
     */
    @GetMapping(value = "allRole.action")
    public String allRole(int pageNum, Model model){
        int page = 1;
        if (pageNum > 0) {
            page = pageNum;
            System.out.println(pageNum);
        }
        List<Role> roles = roleService.allRole(page);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        model.addAttribute("roles", roles);
        model.addAttribute("pageInfo", pageInfo);
        return "/WEB-INF/role/findAllRole.jsp";
    }

    @GetMapping(value = "roleDetailedInformation.action")
    public String allRoleDetailedInformation(int role_id, String role_name, Model model){
        List<Employee> employees = roleService.roleDetailedInformation(role_id);
        model.addAttribute("employees", employees);
        model.addAttribute("role_name", role_name);
        return "/WEB-INF/role/roleDetailedInformation.jsp";
    }

    /**
     * 点击跳转添加角色页面
     * @return 跳转页面
     */
    @GetMapping(value = "appendRoleJump.action")
    public String appendRoleJump(){
        return "/WEB-INF/role/appendRole";
    }

    /**
     * 添加新角色
     * @param role_name 新角色名称
     * @param employee_person 被选中的雇员ID
     * @return 添加结果
     */
    @PostMapping(value = "appendRole.action")
    public String appendRole(String role_name, String employee_person){
        boolean appendRole = roleService.appendRole(role_name, employee_person);
        if(appendRole){
            return "添加成功";
        } else {
            return "添加失败";
        }
    }
}
